package com.unswift;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.unswift.utils.FileUtils;

import net.ifok.image.image4j.codec.ico.ICOEncoder;

public class ImageConvert {

	public static void main(String[] args) {
		String sourceFile="D:\\develop\\application\\unswift-quick\\logo.png";
		String targetFile="D:\\develop\\application\\unswift-quick\\logo.ico";
//		FileUtils.createFile(targetFile, true);
		pngToIcon(sourceFile, targetFile);
	}
	
	public static void pngToIcon(String sourceFile, String targetFile) {
		try {
			BufferedImage pngImage = ImageIO.read(new File(sourceFile));
			Image iconDefault = pngImage.getScaledInstance(32, 32, Image.SCALE_DEFAULT);
			BufferedImage iconImage=new BufferedImage(pngImage.getWidth(), pngImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D graphics = iconImage.createGraphics();
			graphics.drawImage(iconDefault, 0, 0, null);
			graphics.dispose();
			List<BufferedImage> icons = new ArrayList<>();
			icons.add(iconImage);
			ICOEncoder.write(icons,new File(targetFile));
			
//			ImageIO.write(pngImage, "ICO", new File(targetFile));
			System.out.println("生成完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
